package com.yunsc.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunsc.system.dao.YunscStoreMenuDao;
import com.yunsc.system.entity.YunscStoreMenuEntity;
import com.yunsc.system.service.YunscStoreMenuService;
import com.yunsc.system.vo.StoreMenuVO;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("yunscStoreMenuService")
public class YunscStoreMenuServiceImpl extends ServiceImpl<YunscStoreMenuDao, YunscStoreMenuEntity> implements YunscStoreMenuService {


    @Override
    public List<StoreMenuVO> queryMenuList(Integer storeUserId) {
        List<StoreMenuVO> list = this.baseMapper.queryMenuList();
        Map<Integer, List<StoreMenuVO>> menuMap = new HashMap<>();
        for (StoreMenuVO menu : list) {
            menuMap.computeIfAbsent(menu.getParentId(), k -> new ArrayList<>()).add(menu);
        }
        return getMenuChildren(0, menuMap);
    }

    private List<StoreMenuVO> getMenuChildren(Integer menuId, Map<Integer, List<StoreMenuVO>> menuMap) {
        List<StoreMenuVO> children = menuMap.get(menuId);
        if (children != null) {
            for (StoreMenuVO menu : children) {
                menu.setChildren(getMenuChildren(menu.getMenuId(), menuMap));
            }
        }
        return children;
    }

}